package com.future.bantang.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;

import com.future.bantang.app.BanTangAPP;
import com.future.bantang.entity.BanTangHomeEntity.DataEntity.BannerEntity;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 16-1-13.
 */
public class HomeBannerPagerAdapter extends BasePagerAdapter<BannerEntity> {

    private List<ImageView> imageViews;
    private LayoutParams ivParams;
    private ImageLoader mImageLoader;

    public HomeBannerPagerAdapter(Context mContext,List<BannerEntity> list) {
        super(mContext,list);
        imageViews = new ArrayList<>();
        ivParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        mImageLoader= BanTangAPP.getApp().getImageLoader();
    }


    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        if (mList.size()<=0){
            return null;
        }
        position = (mList.size() + position % mList.size()) % mList.size();
        ImageView imageView = null;
        BannerEntity bannerEntity = mList.get(position);
        if (imageViews.size() <= position) {
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(ivParams);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        } else {
            imageView = imageViews.get(position);
            imageViews.add(imageView);
        }
        String url = (String) imageView.getTag();
        if (TextUtils.isEmpty(url)||!TextUtils.equals(url,bannerEntity.getPhoto())){
            imageView.setTag(bannerEntity.getPhoto());
            mImageLoader.displayImage(bannerEntity.getPhoto(), imageView);
        }
        container.addView(imageView);
        return imageView;
    }
}
